/** 物料档案 */
import {Button, Form, Row, Col, Input, Select, message} from 'antd'
import React, {useState, useEffect} from 'react'
import {history} from 'umi'
import styles from './index.less'
import Modal from '@/components/Modal'
import {
	addWarehousegoodsarchives,
	editWarehousegoodsarchives,
	warehousegoodsarchivesInfo,
} from '@/service/storage/material'
import {getGenerateNumber} from '@/service/common'
import {getDict} from '@/service/common/dict'
import OperationRecord from '@/components/OperationRecord'

const add = (props: any) => {
	const {query} = props.location
	const [form] = Form.useForm()
	const [type, setType] = useState<string>()
	const [isModalOpen, setIsModalOpen] = useState(false)
	const [formData, setFormData] = useState<any>({})
	const [dictData, setDictData] = useState<any>({})

	// 根据路由判断当前页面状态
	useEffect(() => {
		getDict(
			{
				dictKeys: [
					'storage_large_classify',
					'storage_middle_classify',
					'Company',
				],
			},
			(res: any) => {
				setDictData(res)
			},
		)
		const hrefs = window.location.href
		if (hrefs.includes('view')) {
			setType('view')
		} else if (hrefs.includes('add')) {
			setType('add')
			getGenerateNumber('WLDA').then((res: any) => {
				form.setFieldsValue({
					goodsCode: res.data,
				})
			})
		} else {
			setType('edit')
		}
	}, [])

	useEffect(() => {
		if (query?.id) {
			warehousegoodsarchivesInfo(query?.id).then((res: any) => {
				if (res?.data) {
					form.setFieldsValue({
						...res.data,
					})
				}
			})
		}
	}, [query])

	const onSave = async () => {
		const value = await form.validateFields()
		if (value) {
			setFormData({
				...value,
			})
			setIsModalOpen(true)
		}
	}

	const onSubmit = () => {
		let request: any
		if (query?.id) {
			// 编辑
			request = editWarehousegoodsarchives({...formData, id: query.id})
		} else {
			request = addWarehousegoodsarchives(formData)
		}
		request
			.then((res: any) => {
				if (res.success) {
					message.success(res.msg)
					history.goBack()
				}
			})
			.catch((err: any) => {
				message.error(err)
			})
	}

	return (
		<div className={styles.add}>
			<div className={styles.title}>基础信息</div>
			<Form
				form={form}
				name="basic"
				layout="horizontal"
				labelCol={{flex: '140px'}}
				labelAlign="right"
				labelWrap
				autoComplete="off"
				disabled={type === 'view'}>
				<Row gutter={12}>
					<Col sm={{span: 24}} md={{span: 12}} lg={{span: 8}}>
						<Form.Item
							label="货物代码"
							name="goodsCode"
							rules={[{required: true}]}>
							<Input placeholder="请输入" disabled />
						</Form.Item>
					</Col>
					<Col sm={{span: 24}} md={{span: 12}} lg={{span: 8}}>
						<Form.Item
							label="名称"
							name="goodsArchivesName"
							rules={[{required: true}]}>
							<Input placeholder="请输入" />
						</Form.Item>
					</Col>
					<Col sm={{span: 24}} md={{span: 12}} lg={{span: 8}}>
						<Form.Item
							label="状态"
							name="archivesStatus"
							rules={[{required: true}]}>
							<Select
								placeholder="请选择"
								allowClear
								options={[
									{value: '1', label: '已启用'},
									{value: '2', label: '未启用'},
								]}
							/>
						</Form.Item>
					</Col>
					<Col sm={{span: 24}} md={{span: 12}} lg={{span: 8}}>
						<Form.Item
							label="大分类"
							name="largeClassify"
							rules={[{required: true}]}>
							<Select
								placeholder="请选择"
								allowClear
								fieldNames={{
									value: 'dictKey',
									label: 'dictValue',
								}}
								options={dictData?.storage_large_classify}
							/>
						</Form.Item>
					</Col>
					<Col sm={{span: 24}} md={{span: 12}} lg={{span: 8}}>
						<Form.Item
							label="中分类"
							name="middleClassify"
							rules={[{required: true}]}>
							<Select
								placeholder="请选择"
								allowClear
								fieldNames={{
									value: 'dictKey',
									label: 'dictValue',
								}}
								options={dictData?.storage_middle_classify}
							/>
						</Form.Item>
					</Col>
					<Col sm={{span: 24}} md={{span: 12}} lg={{span: 8}}>
						<Form.Item
							label="单位"
							name="company"
							rules={[{required: true}]}>
							<Select
								placeholder="请选择"
								allowClear
								fieldNames={{
									value: 'dictKey',
									label: 'dictValue',
								}}
								options={dictData?.Company}
							/>
						</Form.Item>
					</Col>
				</Row>
			</Form>
			{type === 'view' ? (
				<>
					<div className={styles.title}>操作记录</div>
					<OperationRecord businessId={query?.id} businessType={37} />
				</>
			) : (
				''
			)}
			<div className={styles.wrap}>
				{type === 'view' ? (
					<Button
						onClick={() => {
							history.go(-1)
						}}>
						返回
					</Button>
				) : (
					<>
						<Button
							type="primary"
							htmlType="submit"
							onClick={onSave}>
							保存{' '}
						</Button>
						<Button
							onClick={() => {
								history.go(-1)
							}}>
							取消
						</Button>
					</>
				)}
			</div>
			<Modal
				text="确定要保存吗？请仔细核对所填信息"
				onSubmit={onSubmit}
				isModalOpen={isModalOpen}
				setIsModalOpen={setIsModalOpen}
			/>
		</div>
	)
}

export default add
